﻿using System;
using System.Data;
using Model;
using DBUtility;

namespace DAL
{
     /// <summary>
     /// 数据访问层RESULT_NOSIZE_DAL
     /// </summary>
     public class RESULT_NOSIZE_DAL
     {
         public RESULT_NOSIZE_DAL()
         {
         }


         /// <summary>
         /// 得到最大JCLSH
         /// </summary>
         public string GetMax_JCLSH(string p_strWhere)
         {
             string strResult = "0";
             string strSql = "select max(JCLSH) as m from RESULT_NOSIZE";

             if(p_strWhere.Trim().Length > 0)
             {
                 strSql += " where " + p_strWhere;
             }

             DataTable dtTemp = new DataTable();
             DbHelper.GetTable(strSql, ref dtTemp);

             if(dtTemp.Rows.Count>0)
             {
                 if(dtTemp.Rows[0]["m"].ToString().Length>0)
                 {
                     strResult = dtTemp.Rows[0]["m"] == DBNull.Value ? "" : dtTemp.Rows[0]["m"].ToString();
                 }
             }

             dtTemp.Dispose();

             return string.Format("{0:D50}", Convert.ToInt32(strResult) + 1);

          }

          /// <summary>
          /// 得到最大JCLSH
          /// </summary>
          public string GetMax_JCLSH()
          {
              return GetMax_JCLSH("");
          }


          /// <summary>
          /// 判断数据是否存在
          /// </summary>
          public bool Exists(string strJCLSH)
          {
               bool bolResult = false;

               if (strJCLSH == null)
               {
                   return false;
               }

               if (strJCLSH.Length == 0)
               {
                   return false;
               }

               string strSql = "";
               strSql += "select count(1) as c from RESULT_NOSIZE";
               strSql += " where ";
               strSql += " JCLSH='"+ strJCLSH +"'";

               DataTable dtTemp = new DataTable();
               DbHelper.GetTable(strSql, ref dtTemp);

               if (dtTemp.Rows.Count > 0)
               {
                   bolResult = Convert.ToInt32(dtTemp.Rows[0]["c"]) > 0;
               }

               dtTemp.Dispose();

               return bolResult;

          }

          /// <summary>
          /// 获取数据总记录数
          /// </summary>
          public int GetRecordCount(string p_strWhere)
          {
              int intResult=0;

              string strSql = "";
              strSql += "select count(1) as c from RESULT_NOSIZE";
              if(p_strWhere.Trim().Length > 0)
              {
                 strSql += " where " + p_strWhere;
              }

              DataTable dtTemp = new DataTable();
              DbHelper.GetTable(strSql, ref dtTemp);

              if(dtTemp.Rows.Count>0)
              {
                  intResult=Convert.ToInt32(dtTemp.Rows[0]["c"]);
              }

              dtTemp.Dispose();

              return intResult;
          }

          /// <summary>
          /// 获取数据总记录数
          /// </summary>
          public int GetRecordCount()
          {
              return GetRecordCount("");
          }

          /// <summary>
          /// 获取数据分页总数
           /// </summary>
          public int GetPageCount(string p_strWhere, int p_intPageSize)
          {
              int intResult=0;

              if(p_intPageSize > 0)
              {
                  int intRecordCount = GetRecordCount(p_strWhere);
                  intResult = Convert.ToInt32(Math.Truncate(Convert.ToDecimal(intRecordCount) / Convert.ToDecimal(p_intPageSize)));
                  if (intRecordCount > (intResult * p_intPageSize))
                  {
                       intResult++;
                  }
              }

              if(intResult == 0)
              {
                   intResult++;
              }
              return intResult;
           }

          /// <summary>
          /// 获取数据分页总数
          /// </summary>
          public int GetPageCount(int p_intPageSize)
          {
              return GetPageCount("", p_intPageSize);
          }

          /// <summary>
          /// 添加一条数据 SQL
          /// </summary>
          public string InsertSQL(RESULT_NOSIZE model)
          {
              string strFieldSQL = "";
              string strValueSQL = "";

              if(model.Changed("ZYJCCS") == true && model.ZYJCCS != null)
              {
                   strFieldSQL += ",ZYJCCS";
                   strValueSQL += ",'" + model.ZYJCCS.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("JCLSH") == true && model.JCLSH != null)
              {
                   strFieldSQL += ",JCLSH";
                   strValueSQL += ",'" + model.JCLSH.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("HJZYZ") == true && model.HJZYZ != null)
              {
                   strFieldSQL += ",HJZYZ";
                   strValueSQL += ",'" + model.HJZYZ.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("SCZYZ") == true && model.SCZYZ != null)
              {
                   strFieldSQL += ",SCZYZ";
                   strValueSQL += ",'" + model.SCZYZ.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYZ_PD") == true && model.ZYZ_PD != null)
              {
                   strFieldSQL += ",ZYZ_PD";
                   strValueSQL += ",'" + model.ZYZ_PD.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYXZXX") == true && model.ZYXZXX != null)
              {
                   strFieldSQL += ",ZYXZXX";
                   strValueSQL += ",'" + model.ZYXZXX.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYXZSX") == true && model.ZYXZSX != null)
              {
                   strFieldSQL += ",ZYXZSX";
                   strValueSQL += ",'" + model.ZYXZSX.ToString().Replace("'","''") + "'";
              }

              string strSql = "";
              strSql += "insert into RESULT_NOSIZE";
              strSql += "(";
              strSql += strFieldSQL.Substring(1);
              strSql += ")";
              strSql += " values";
              strSql += "(";
              strSql += strValueSQL.Substring(1);
              strSql += ")";

              return strSql;
          }

          /// <summary>
          /// 添加一条数据
          /// </summary>
          public bool Insert(RESULT_NOSIZE model)
          {
              return DbHelper.ExecuteSql(InsertSQL(model));
          }

          /// <summary>
          /// 修改一条数据 SQL
          /// </summary>
          public string UpdateSQL(RESULT_NOSIZE model,string strJCLSH)
          {
              string strUpdateSQL = "";

              if(model.Changed("ZYJCCS") == true && model.ZYJCCS != null)
              {
                  strUpdateSQL += ",ZYJCCS='" + model.ZYJCCS.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("JCLSH") == true && model.JCLSH != null)
              {
                  strUpdateSQL += ",JCLSH='" + model.JCLSH.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("HJZYZ") == true && model.HJZYZ != null)
              {
                  strUpdateSQL += ",HJZYZ='" + model.HJZYZ.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("SCZYZ") == true && model.SCZYZ != null)
              {
                  strUpdateSQL += ",SCZYZ='" + model.SCZYZ.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYZ_PD") == true && model.ZYZ_PD != null)
              {
                  strUpdateSQL += ",ZYZ_PD='" + model.ZYZ_PD.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYXZXX") == true && model.ZYXZXX != null)
              {
                  strUpdateSQL += ",ZYXZXX='" + model.ZYXZXX.ToString().Replace("'","''") + "'";
              }

              if(model.Changed("ZYXZSX") == true && model.ZYXZSX != null)
              {
                  strUpdateSQL += ",ZYXZSX='" + model.ZYXZSX.ToString().Replace("'","''") + "'";
              }

               string strSql = "";
               strSql += "update RESULT_NOSIZE set ";
               strSql += strUpdateSQL.Substring(1);
               strSql += " where ";
               strSql += " JCLSH='"+ strJCLSH +"'";

               return strSql;
          }

          /// <summary>
          /// 修改一条数据
          /// </summary>
          public bool Update(RESULT_NOSIZE model, string strJCLSH)
          {
              return DbHelper.ExecuteSql(UpdateSQL(model, strJCLSH));
          }

          /// <summary>
          /// 修改一个集合 SQL
          /// </summary>
           public string UpdateRangeSQL(RESULT_NOSIZE model, string p_strWhere)
          {
               string strUpdateSQL = "";

               if(model.Changed("ZYJCCS") == true && model.ZYJCCS != null)
               {
                  strUpdateSQL += ",ZYJCCS='" + model.ZYJCCS.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("JCLSH") == true && model.JCLSH != null)
               {
                  strUpdateSQL += ",JCLSH='" + model.JCLSH.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("HJZYZ") == true && model.HJZYZ != null)
               {
                  strUpdateSQL += ",HJZYZ='" + model.HJZYZ.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("SCZYZ") == true && model.SCZYZ != null)
               {
                  strUpdateSQL += ",SCZYZ='" + model.SCZYZ.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("ZYZ_PD") == true && model.ZYZ_PD != null)
               {
                  strUpdateSQL += ",ZYZ_PD='" + model.ZYZ_PD.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("ZYXZXX") == true && model.ZYXZXX != null)
               {
                  strUpdateSQL += ",ZYXZXX='" + model.ZYXZXX.ToString().Replace("'","''") + "'";
               }

               if(model.Changed("ZYXZSX") == true && model.ZYXZSX != null)
               {
                  strUpdateSQL += ",ZYXZSX='" + model.ZYXZSX.ToString().Replace("'","''") + "'";
               }

              string strSql = "";
              strSql += "update RESULT_NOSIZE set ";
              strSql += strUpdateSQL.Substring(1);
              strSql += " where " + p_strWhere;

              return strSql;
          }

          /// <summary>
          /// 修改一个集合
          /// </summary>
          public bool UpdateRange(RESULT_NOSIZE model, string p_strWhere)
          {
              return DbHelper.ExecuteSql(UpdateRangeSQL(model, p_strWhere));
          }

          /// <summary>
          /// 修改全部数据 SQL
          /// </summary>
          public string UpdateAllSQL(RESULT_NOSIZE model)
          {
              string strUpdateSQL = "";

                  strUpdateSQL += ",ZYJCCS='" + model.ZYJCCS.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",JCLSH='" + model.JCLSH.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",HJZYZ='" + model.HJZYZ.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",SCZYZ='" + model.SCZYZ.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",ZYZ_PD='" + model.ZYZ_PD.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",ZYXZXX='" + model.ZYXZXX.ToString().Replace("'","''") + "'";
                  strUpdateSQL += ",ZYXZSX='" + model.ZYXZSX.ToString().Replace("'","''") + "'";


               string strSql = "";
               strSql += "update RESULT_NOSIZE set ";
               strSql += strUpdateSQL.Substring(1);

               return strSql;

          }

          /// <summary>
          /// 修改全部数据
          /// </summary>
          public bool UpdateAll(RESULT_NOSIZE model)
          {
              return DbHelper.ExecuteSql(UpdateAllSQL(model));
          }

          /// <summary>
          /// 删除一条数据 SQL
          /// </summary>
          public string DeleteSQL(string strJCLSH)
          {
              string strSql = "";
              strSql += "delete from RESULT_NOSIZE";
              strSql += " where ";
               strSql += " JCLSH='"+ strJCLSH +"'";

              return strSql;

          }

          /// <summary>
          /// 删除一条数据
          /// </summary>
          public bool Delete(string strJCLSH)
          {
              return DbHelper.ExecuteSql(DeleteSQL(strJCLSH));
          }

          /// <summary>
          /// 删除一个集合 SQL
          /// </summary>
          public string DeleteRangeSQL(string p_strWhere)
          {
              string strSql = "";
              strSql += "delete from RESULT_NOSIZE";
              strSql += " where " + p_strWhere;

              return strSql;
           }

          /// <summary>
          /// 删除一个集合
          /// </summary>
          public bool DeleteRange(string p_strWhere)
          {
              return DbHelper.ExecuteSql(DeleteRangeSQL(p_strWhere));
          }

          /// <summary>
          /// 删除全部 SQL
          /// </summary>
          public string DeleteAllSQL()
          {
              string strSql = "";
              strSql += "delete from RESULT_NOSIZE";

              return strSql;
           }

         /// <summary>
         /// 删除全部
         /// </summary>
         public bool DeleteAll()
         {
             return DbHelper.ExecuteSql(DeleteAllSQL());
         }

          /// <summary>
         /// 得到一个对象实体
         /// </summary>
         public RESULT_NOSIZE GetModel(string strJCLSH)
         {
             string strSql = "";
             strSql += "select * from RESULT_NOSIZE";
             strSql += " where ";
               strSql += " JCLSH='"+ strJCLSH +"'";

             DataTable dtTemp = new DataTable();
             DbHelper.GetTable(strSql, ref dtTemp);

             RESULT_NOSIZE model = new RESULT_NOSIZE();

             if(dtTemp.Rows.Count>0)
             {
                 model = new RESULT_NOSIZE();

                 model.ID = dtTemp.Rows[0]["ID"] == DBNull.Value ? 0 : Convert.ToInt32(dtTemp.Rows[0]["ID"]);
                 model.ZYJCCS = dtTemp.Rows[0]["ZYJCCS"] == DBNull.Value ? "" : dtTemp.Rows[0]["ZYJCCS"].ToString();
                 model.JCLSH = dtTemp.Rows[0]["JCLSH"] == DBNull.Value ? "" : dtTemp.Rows[0]["JCLSH"].ToString();
                 model.HJZYZ = dtTemp.Rows[0]["HJZYZ"] == DBNull.Value ? "" : dtTemp.Rows[0]["HJZYZ"].ToString();
                 model.SCZYZ = dtTemp.Rows[0]["SCZYZ"] == DBNull.Value ? "" : dtTemp.Rows[0]["SCZYZ"].ToString();
                 model.ZYZ_PD = dtTemp.Rows[0]["ZYZ_PD"] == DBNull.Value ? "" : dtTemp.Rows[0]["ZYZ_PD"].ToString();
                 model.ZYXZXX = dtTemp.Rows[0]["ZYXZXX"] == DBNull.Value ? "" : dtTemp.Rows[0]["ZYXZXX"].ToString();
                 model.ZYXZSX = dtTemp.Rows[0]["ZYXZSX"] == DBNull.Value ? "" : dtTemp.Rows[0]["ZYXZSX"].ToString();
             }

             dtTemp.Dispose();

             return model;
         }

         /// <summary>
         /// 得到一个对象实体
         /// </summary>
         public void GetModel(ref DataTable p_dtData, string strJCLSH)
         {
             p_dtData.Clear();

             string strSql = "";
             strSql += "select * from RESULT_NOSIZE";
             strSql += " where ";
               strSql += " JCLSH='"+ strJCLSH +"'";

              DbHelper.GetTable(strSql, ref p_dtData);
         }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList(string p_strWhere, string p_strOrder, int p_intPageNumber, int p_intPageSize)
      {
          int m_intPageNumber = p_intPageNumber;
          int m_intPageCount = GetPageCount(p_strWhere, p_intPageSize);

          if((m_intPageNumber < 1) || (m_intPageNumber == 0))
          {
               m_intPageNumber = 1;
          }

          if(m_intPageNumber == -1)
          {
               m_intPageNumber = m_intPageCount;
          }

          if(m_intPageNumber > m_intPageCount)
          {
               m_intPageNumber = m_intPageCount;
          }

          string strSql = "";
          strSql += "select * from RESULT_NOSIZE";
          if(p_strWhere.Trim().Length > 0)
          {
               strSql += " where " + p_strWhere;
          }
          if(p_strOrder.Trim().Length > 0)
          {
               strSql += " order by " + p_strOrder;
          }

          DataTable dtTemp = new DataTable();

          if(p_intPageSize > 0)
          {
               DbHelper.GetTable(strSql, ref dtTemp, (m_intPageNumber-1)*p_intPageSize, p_intPageSize);
          }
          else
          {
               DbHelper.GetTable(strSql, ref dtTemp);
          }

          RESULT_NOSIZE[] arrModel=new RESULT_NOSIZE[dtTemp.Rows.Count];

          for(int N=0;N<dtTemp.Rows.Count;N++)
          {
               arrModel[N] = new RESULT_NOSIZE();

                 arrModel[N].ID = dtTemp.Rows[N]["ID"] == DBNull.Value ? 0 : Convert.ToInt32(dtTemp.Rows[N]["ID"]);
                 arrModel[N].ZYJCCS = dtTemp.Rows[N]["ZYJCCS"] == DBNull.Value ? "" : dtTemp.Rows[N]["ZYJCCS"].ToString();
                 arrModel[N].JCLSH = dtTemp.Rows[N]["JCLSH"] == DBNull.Value ? "" : dtTemp.Rows[N]["JCLSH"].ToString();
                 arrModel[N].HJZYZ = dtTemp.Rows[N]["HJZYZ"] == DBNull.Value ? "" : dtTemp.Rows[N]["HJZYZ"].ToString();
                 arrModel[N].SCZYZ = dtTemp.Rows[N]["SCZYZ"] == DBNull.Value ? "" : dtTemp.Rows[N]["SCZYZ"].ToString();
                 arrModel[N].ZYZ_PD = dtTemp.Rows[N]["ZYZ_PD"] == DBNull.Value ? "" : dtTemp.Rows[N]["ZYZ_PD"].ToString();
                 arrModel[N].ZYXZXX = dtTemp.Rows[N]["ZYXZXX"] == DBNull.Value ? "" : dtTemp.Rows[N]["ZYXZXX"].ToString();
                 arrModel[N].ZYXZSX = dtTemp.Rows[N]["ZYXZSX"] == DBNull.Value ? "" : dtTemp.Rows[N]["ZYXZSX"].ToString();
          }

          dtTemp.Dispose();

          return arrModel;
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList(string p_strWhere)
      {
          return GetModelList(p_strWhere, "", -1, -1);
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList(string p_strWhere, string p_strOrder)
      {
          return GetModelList(p_strWhere, p_strOrder, -1, -1);
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList(int p_intPageNumber, int p_intPageSize)
      {
          return GetModelList("", "", p_intPageNumber, p_intPageSize);
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList(string p_strWhere, int p_intPageNumber, int p_intPageSize)
      {
          return GetModelList(p_strWhere, "", p_intPageNumber, p_intPageSize);
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public RESULT_NOSIZE[] GetModelList()
      {
          return GetModelList("", "", -1, -1);
      }

      /// <summary>
      /// 得到对象集合
      /// </summary>
      public void GetModelList(ref DataTable p_dtData, string p_strWhere, string p_strOrder, int p_intPageNumber, int p_intPageSize)
      {
          p_dtData.Clear();

          int m_intPageNumber = p_intPageNumber;
          int m_intPageCount = GetPageCount(p_strWhere, p_intPageSize);

          if((m_intPageNumber < 1) || (m_intPageNumber == 0))
          {
              m_intPageNumber = 1;
          }

          if(m_intPageNumber == -1)
          {
              m_intPageNumber = m_intPageCount;
          }

          if(m_intPageNumber > m_intPageCount)
          {
              m_intPageNumber = m_intPageCount;
          }

          string strSql = "";
          strSql += "select * from RESULT_NOSIZE";
          if(p_strWhere.Trim().Length > 0)
          {
              strSql += " where " + p_strWhere;
          }
          if(p_strOrder.Trim().Length > 0)
          {
              strSql += " order by " + p_strOrder;
          }

          if(p_intPageSize > 0)
          {
              DbHelper.GetTable(strSql, ref p_dtData, (m_intPageNumber-1)*p_intPageSize, p_intPageSize);
          }
          else
          {
              DbHelper.GetTable(strSql, ref p_dtData);
          }
       }

       /// <summary>
       /// 得到对象集合
       /// </summary>
       public void GetModelList(ref DataTable p_dtData, string p_strWhere)
       {
           GetModelList(ref p_dtData, p_strWhere, "", -1, -1);
       }

       /// <summary>
       /// 得到对象集合
       /// </summary>
       public void GetModelList(ref DataTable p_dtData, string p_strWhere, string p_strOrder)
       {
           GetModelList(ref p_dtData, p_strWhere, p_strOrder, -1, -1);
       }

       /// <summary>
       /// 得到对象集合
       /// </summary>
       public void GetModelList(ref DataTable p_dtData, int p_intPageNumber, int p_intPageSize)
       {
           GetModelList(ref p_dtData, "", "", p_intPageNumber, p_intPageSize);
       }

       /// <summary>
       /// 得到对象集合
       /// </summary>
       public void GetModelList(ref DataTable p_dtData, string p_strWhere, int p_intPageNumber, int p_intPageSize)
       {
           GetModelList(ref p_dtData, p_strWhere, "", p_intPageNumber, p_intPageSize);
       }
       /// <summary>
       /// 得到对象集合
       /// </summary>
       public void GetModelList(ref DataTable p_dtData)
       {
           GetModelList(ref p_dtData, "", "", -1, -1);
       }
   }
}
